package mp

import (
	"duoke/model"
	"duoke/service"
	"duoke/tables"
	"duoke/utils/gconv"
	"duoke/utils/orm"
	"duoke/utils/request"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
)

var Goods = new(goodsCtl)

type goodsCtl struct{}

func (ctl *goodsCtl) List(c *gin.Context) {
	homeSetting, err := service.AccountSetting.Info(c, "home")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	list := make([]model.ReqGoods, 0)
	err = orm.Db.In("id", homeSetting["goods_ids"]).Desc("id").Find(&list)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", list)
}
func (ctl *goodsCtl) Info(c *gin.Context) {
	goods, _ := service.Request.Goods(c)
	err := service.Goods.Views(goods)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", goods)
}
func (ctl *goodsCtl) InfoSimple(c *gin.Context) {
	req := request.JsonMap(c)
	goods, err := service.Goods.GetById(gconv.Int(req["goodsId"]), "")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", goods)
}
func (ctl *goodsCtl) MyOrder(c *gin.Context) {
	goods, _ := service.Request.Goods(c)
	goodsUser, _ := service.Request.GoodsUser(c)
	where := "goods_id = " + gconv.String(goods.Id)
	where += " AND pay_state = 1"
	where += " AND user_id = " + gconv.String(goodsUser.UserId)
	//fmt.Println(where)
	order, _ := service.Order.GetByWhere(where, "id,name,nickname,avatar,group_num,group_id,is_head,is_new_old,head_user_id")
	response.Success(c, "", order)
}

// 用户行为
func (ctl *goodsCtl) Behavior(c *gin.Context) {
	goods, _ := service.Request.Goods(c)
	log := make([]tables.Behavior, 0)
	err := orm.Db.Where("goods_id =?", goods.Id).Distinct("nickname,avatar,action", "user_id").Limit(100).Desc("id").Find(&log)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", log)
}
